﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SendMailServer
{
    internal class MarkDownFile
    {
        public static ResMsg HandlerMsg(string key, string filterStr)
        {
            if (key != "downladMarkdownFile"
                && key != "saveMarkdownFile"
                && key != "getMarkdownFileChanged")
                return null;

            if (filterStr == null) { filterStr = ""; }
            if (filterStr.EndsWith(",desktop"))
            {
                filterStr = filterStr.Substring(0, filterStr.Length - ",desktop".Length);
            }

            if (key == "downladMarkdownFile")
            {
                var res = GetMdFile(filterStr);

                return new ResMsg()
                {
                    MsgCode = key,
                    AppName = key,
                    Content = res.ToString(),
                    IsOk = true,
                    UseSelfResponse = true,
                };
            }
            if (key == "getMarkdownFileChanged")
            {
                var res = GetMdChangedFileCount();
                return new ResMsg()
                {
                    MsgCode = key,
                    AppName = key,
                    Content = res.ToString(),
                    IsOk = true,
                    UseSelfResponse = true,
                };
            }
            if (key == "saveMarkdownFile")
            {
                var msg = SaveMdFile(filterStr);

                return new ResMsg()
                {
                    MsgCode = key,
                    AppName = key,
                    Content = msg,
                    IsOk = true,
                    UseSelfResponse = true,
                };
            }
            return null;
        }
        private static string GetMdFile(string fileName)
        {
            var searchDir = "";
            if (fileName.StartsWith("desktopDoc"))
            {
                fileName = fileName.Substring(fileName.IndexOf('/'));
                fileName = fileName.TrimStart('/');
                var fullFileName = Path.Combine(GSetting.desktopDocDir, fileName);
                var editFile = Path.ChangeExtension(fullFileName, ".mdedit");
                searchDir += fullFileName + " and " + editFile;
                if (File.Exists(editFile))
                {
                    return File.ReadAllText(editFile);
                }
                if (File.Exists(fullFileName))
                {
                    return File.ReadAllText(fullFileName);
                }
            }
            
            return "未找到文件:"+ searchDir;
        }
        private static string SaveMdFile(string fileNameWithContent)
        {
            var t = fileNameWithContent.IndexOf(';');
            var fileName = fileNameWithContent.Substring(0, t);
            var fileContent= fileNameWithContent.Substring(t+1); 
            var searchDir = "";
            if (fileName.StartsWith("desktopDoc"))
            {
                fileName = fileName.Substring(fileName.IndexOf('/'));
                fileName = fileName.TrimStart('/');
                var fullFileName = Path.Combine(GSetting.desktopDocDir, fileName);
                var editFile = Path.ChangeExtension(fullFileName, ".mdedit");
                searchDir += fullFileName + " and " + editFile;
               
                File.WriteAllText(editFile, fileContent);

                return "提交成功，感谢您的贡献";
            }

            return "未找到文件:" + searchDir;
        }

        private static string GetMdChangedFileCount()
        {
            var allFiles = new DirectoryInfo(GSetting.desktopDocDir).GetFiles("*.mdedit", SearchOption.AllDirectories);
            return allFiles.Length.ToString();
        }
    }
}
